<?php
/**
 * @version $Id$
 * @copyright 2005 
 **/
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
require_once("../../config.php");
require_once("lib.php");

set_time_limit(0);

$id = $_GET['id'];
$w = $_GET['w'];

require_variable($id); 
require_variable($w); 

if (!$assignment = get_record("aca", "id", $id)) {
    error("Course module is incorrect");
}
if (! $course = get_record("course", "id", $assignment->course)) {
    error("Course is misconfigured");
}
if (! $cm = get_coursemodule_from_instance("aca", $assignment->id, $course->id)) {
    error("Course Module ID was incorrect");
}

require_login($course->id, false, $cm);

print_header("Výsledky zpracování");

require($CFG->dirroot.'/mod/aca/type/'.$assignment->langtype.'/aca.class.php');
$acaclass = 'aca_'.$assignment->langtype;
$acainstance = new $acaclass($cm->id, $assignment, $cm, $course);

if($w=="maketokens"){
	if($acainstance->MakeTokens($id)){
		$assignment=get_record("aca","id",$id);
		$ass_name=$assignment->name;
		$output = "<h2>Soubory tokenů pro úkol $ass_name byly úspěšně vytvořeny.</h2>";
		$output .= "<h3><a href=\"#\" onclick=\"window.opener.location.replace(window.opener.location.href);self.close();return false;\">Zavřít</a></h3>";
		print_simple_box($output,'center');
	}
}
elseif($w=="execyap"){
	$acainstance->process_all_pds($id);
	$output = "<h3>Proces úspěšně dokončen.</h3><h3><a href=\"#\" onclick=\"window.opener.location.replace(window.opener.location.href);self.close();return false;\">Zavřít</a></h3>";
	print_simple_box($output,'center');
}
elseif($w=="loadyap"){
	LoadYapSummary($id);
}
elseif($w=="export"){
    //echo "export $id";
    @unlink("$ACACFG->newpath/$id/yap.newfiles");
    @unlink("$ACACFG->newpath/$id/yap.numbers");
    @unlink("$ACACFG->newpath/$id/yap.status");
    @unlink("$ACACFG->newpath/$id/yap.submissions");
    @unlink("$ACACFG->newpath/$id/yap.summary");
    $dir=opendir("$ACACFG->newpath/$id");
    if(!$dir)die("cannot open directory with tokens");
    while(($file=readdir($dir))!==false){
	if(is_file("$ACACFG->newpath/$id/$file"))$files[]="$ACACFG->newpath/$id/$file";
	
        
    }
//    foreach($files as $re)echo "[$re]";
    $ass=get_record("aca","id",$id);
    
    $newfile="$ass->course/$CFG->moddata/aca/tokens-$id-".date("d-m-Y").".zip";
//    echo "[$CFG->dataroot/$newfile]";
    zip_files($files,"$CFG->dataroot/$newfile");
    header("Location: $CFG->wwwroot/file.php/$newfile");
}
elseif($w=="import"){
    echo "import $id";
    echo "<form action=pd4m.make.php method=post enctype=\"multipart/form-data\">";
    echo "<input type=hidden name=MAX_FILE_SIZE value=1000000>";
    echo "<input type=hidden name=w value=importtokens>";
    echo "<input type=hidden name=id value=$id>";
    echo "Zip-File:<input type=file name=userfile>";
    echo "<input type=submit value=Import></form>";
}	
elseif($w=="importtokens"){
    echo "import".$_FILES['userfile']['name'];
    if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
	//echo "isfile";
	@unlink("$ACACFG->tmppath/import.zip");
	move_uploaded_file($_FILES['userfile']['tmp_name'],"$ACACFG->tmppath/import.zip");
	chmod("$ACACFG->tmppath/import.zip",0777);
//	echo "[$PD4MCFG->subpath/$id]";
	unzip_file("$ACACFG->tmppath/import.zip","$ACACFG->newpath/$id",false);
	
	$output = "<h2>Soubory tokenů byly úspěšně importovány.</h2>";
	print_simple_box($output,'center');
    }
    
    $output =  "<h3><a href=\"#\" onclick=\"window.opener.location.replace(window.opener.location.href);self.close();return false;\">Zavřít</a></h3>";
    print_simple_box($output,'center');
}

print_footer('none');

/*
echo "<br><br><a href=\"?w=maketokens&assignment=1\">make tokens</a><br>";
echo "<a href=\"?w=execyap&assignment=1\">exec yap</a><br>";
echo "<a href=\"?w=loadyap&assignment=1\">load yap summary</a><br>";
*/
?>
